package boundary;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JProgressBar;
import javax.swing.JLabel;
import javax.swing.JWindow;

import java.awt.Font;
import java.awt.image.BufferedImage;

import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import java.awt.Color;

public class Splash extends JWindow {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JProgressBar jProgressBar = null;
	private JLabel jLabel = null;
	private JPanel jPanel = null;

	/**
	 * @param owner
	 */
	public Splash() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (d.width-300)/2;
		int y = (d.height-200)/2;
		this.setSize(300, 200);
		this.setBounds(x, y, 300, 200);
		this.setContentPane(getJContentPane());
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("Attendere caricamento..");
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jContentPane.add(getJProgressBar(), BorderLayout.SOUTH);
			jContentPane.add(jLabel, BorderLayout.NORTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jProgressBar	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar();
			jProgressBar.setStringPainted(true);
		}
		return jProgressBar;
	}
	
	public void updateBar(int value, String string) {
		getJProgressBar().setValue(value);
		getJProgressBar().setString(string);
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new ImagePanel("/icone/logoo.jpg");
			jPanel.setLayout(new GridBagLayout());
		}
		return jPanel;
	}
	
	private class ImagePanel extends JPanel
	{
		private static final long serialVersionUID = 1101187644420367408L;
		private BufferedImage image = null;
		@SuppressWarnings("unused")
		private String filename;
		
		private ImagePanel(String filename) {
			this.filename = filename;
			try {
				image = ImageIO.read(getClass().getResource(filename));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void paint(Graphics g) {
			Dimension d = getSize();
			int x =( d.width-image.getWidth())/2;
			int y =( d.height-image.getHeight())/2;
			g.drawImage(image, x, y, null);
		}
	}

}
